﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.ServiceProcess;
using DAL;


namespace DAL
{
    
    public partial class TaskProceso
    {


        public int Add(dbGestionDataContext _db,
                        string descripcion,
                        int idObra,
                        int idEmpleado,
                        int tipoTask,
                        int idValorRegistro)
        {
            this.idTipoTask = tipoTask; 
            this.descripcion = descripcion;
            this.idEstadoTask = 16;
            this.porcentaje = 0;
            this.valor = "Import";
            this.idObra = idObra;
            this.usuarioCre = idEmpleado;
            this.fechaCre = DateTime.Now;
            this.idPresupuesto = idValorRegistro;

            _db.TaskProceso.InsertOnSubmit(this);
            _db.SubmitChanges();
            return this.idinternal;
        }

        public void terminarProceso(dbGestionDataContext _db)
        {
            string completo = "100";
            this.porcentaje = decimal.Parse(completo);
            this.idEstadoTask = 18; //terminado
            _db.SubmitChanges();
        }

        public void inicioProceso(dbGestionDataContext _db)
        {
            
            this.idEstadoTask = 17;
            this.porcentaje = decimal.Parse("25");
            _db.SubmitChanges();
        }

        /// <summary>
        /// Favoroite field, fovaorites are displayed on the context menu of the notify icon.
        /// </summary>
        private bool favorite = false;

        /// <summary>
        /// Consturcts a new service wrapper.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="isFavorite"></param>
        //public TaskProceso(ServiceController service,bool isFavorite)
        //{
        //    //this.ServiceController = service;
        //    this.ServiceController = service;
        //    this.favorite = isFavorite;
        //    //LoadDescription();
        //}

        //private void LoadDescription(dbGestionDataContext _db)
        //{
        //    var reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System").OpenSubKey("CurrentControlSet").OpenSubKey("Services").OpenSubKey(ServiceController.ServiceName);
        //    //string description = reg.GetValue("Description") as string;
        //    var q= from a in _db.TaskProceso(porcentaje=porcentaje>==this.idinternal);
                


        //    if (description == null)
        //    {
        //        description = string.Empty;
        //    }
        //    if (description.StartsWith("@"))
        //    {
        //        description = ResourceHelper.LoadWin32Resource(description);
        //    }
        //    reg.Close();
        //    Description = description;
        //}

      

        /// <summary>
        /// Property for is running, changing the state stops/starts the service.
        /// </summary>
        public bool IsRunning
        {
            get
            {
                return this.ServiceController.Status == ServiceControllerStatus.Running;

            }
            set
            {
                if (value)
                {
                    this.Start();
                }
                else
                {
                    this.Stop();
                }

            }

        }

        /// <summary>
        /// Starts, stops the service.
        /// </summary>
        public void Start()
        {
            ServiceController.Refresh();
            if (ServiceController.Status == ServiceControllerStatus.Stopped)
            {
                ServiceController.Start();
            }
            else
            {

            }
        }

        /// <summary>
        /// Stops the service.
        /// </summary>
        public void Stop()
        {
            ServiceController.Refresh();
            if (ServiceController.Status == ServiceControllerStatus.Running)
            {
                ServiceController.Stop();
            }
        }

        /// <summary>
        /// Display name
        /// </summary>
        public string DisplayName
        {
            get
            {
                return this.ServiceController.DisplayName;
            }
        }

        /// <summary>
        /// Status
        /// </summary>
        public ServiceControllerStatus Status
        {
            get
            {
                return this.ServiceController.Status;
            }
        }

        /// <summary>
        /// The service controllers.
        /// </summary>
        public ServiceController ServiceController
        {
            get;
            private set;
        }
    }

}
